define(['app', 'js/directive/listTree', 'js/filter/tree2Arr', 'datetime', 'pagination'], function (app) {
  'use strict';
  /*渠道操作管理*/
  app.controller('Source', function ($scope, dataFactoryCRM, $q, $compile, $timeout) {
    $scope.sector;
    $scope.search_param={name:'',company:-1};
    $scope.sectorlist = {};
    $scope.sourceEdit;
    $scope.sourcepage = {
      pageAll: 1,
      pageNow: 1,
      pageSize: $scope.psize,
      k: 32
    };

    /*系统错误调用*/
    $scope.msgAttention = function () {
      $('.ui.systemerror').modal({
        closable: true,
        onDeny: function () {
          return true;
        },
        onApprove: function () {
          $('.ui.dimmer.modals.page.transition.visible.active.dimmers').removeClass('dimmers');
        }
      }).modal('show');

      setTimeout(function () {
        var system = $('.ui.dimmer.modals.page.transition.visible.active');
        if (system) {
          $('.ui.dimmer.modals.page.transition.visible.active').addClass('dimmers');
        }
      }, 1000);
    }

    $scope.getSector = function(){
        dataFactoryCRM.getDataByAPI('Source/getSector')
            .then(function (d) {
              if (d.rcode) {
                $scope.sectorlist = d.data;
              }
            });
        };

    // sourceDataList list show
    $scope.sourceDataList = function () {
        var sUrl = "Source/index";
        var pm = $scope.search_param;
        pm['psize'] = $scope.sourcepage.pageSize;
        pm['p'] = $scope.sourcepage.pageNow;
        sUrl += '?' + $.param(pm);
        dataFactoryCRM.getDataByAPI(sUrl).then(function (_d) {
            if (_d.rcode == 1) {
              $scope.sourcepage.pageSize = _d.psize;
              $scope.sourcepage.pageAll = _d.pall;
              $scope.sourcepage.pageNow = _d.p;
              $scope.source = _d.data;
            } else {
              $scope.sourcepage.pageAll = 0;
            }
        });
    }
   //初始化页面显示数据（如果是新弹出独立窗口则从父窗口获取）
    window.loadClientCacheToScope($scope, function() {
      $scope.getSector();
      $scope.sourceDataList();
    });
   
    $scope.msg_ok = function( _msg ){
        $('.ui.modal.modal1').remove();
        $('<div class="ui small modal modal1" style="text-align: center;"><div class="header">提示</div><div class="content"><i class="huge green check circle outline icon"></i>' +
                '<p style="padding-top: 15px;">'+_msg+'</p>' +'</div></div>'
            ).modal('show');
    };

    $scope.api_error = function ( msg ) {
        $('<div class="ui small modal modal2" style="text-align: center;"><div class="header">提示</div><div class="content"><i class="huge red warning outline icon"></i>' +
            '<p style="padding-top: 15px;">'+msg+'</p>' +'</div></div>'
        ).modal('show');
    };

    // add source
    $scope.addSource = function () {
        function add(){
            dataFactoryCRM.postDataByAPI('Source/add', $scope.add_data)
                        .then(function(d) {
                            if (d&&d.rcode) {
                                $('.ui.add.modal').modal('hide');
                                $scope.msg_ok("渠道添加成功");
                                $timeout(function(){  $scope.sourceDataList(); }, 5);
                                return true;
                            }
                            else {
                                $scope.api_error(d.msg);
                                return false;
                            }
                        },function( err ){
                            $scope.api_error("接口请求错误");
                        });
        }

        $('#ChannalID').form('reset');
        $('.ui.add.modal').modal({
            onApprove: function () {
              if ($('#ChannalID').form('is valid')) {
                    add();
                    return false;
              } else
                return false;
            }
          }).modal('show');
    }

    //edit source records
    $scope.editSourceBox = function ( sid ) {
        dataFactoryCRM.getDataByAPI('Source/edit?id=' + sid).then( function(d) {
            if ( d&&d.rcode==1) {
                $scope.editData = d.data;
                showEdit();
            }
            else {
                $scope.api_error(d.msg);
            }
        },
        function (err){
            $scope.api_error('接口请求失败');
        }
        );
        function doEdit(){
            dataFactoryCRM.postDataByAPI('Source/edit', $scope.editData)
                        .then(function(d) {
                            if (d&&d.rcode) {
                                $('.ui.edit.modal').modal('hide');
                                $scope.msg_ok("渠道数据修改成功");
                                $timeout(function(){  $scope.sourceDataList(); }, 5);
                                return true;
                            }
                            else{
                                $scope.api_error(d.msg);
                                return false;
                            }
                        },function( err ){
                            $scope.api_error("接口请求错误");
                        });
        }

        function showEdit(){
            $('.ui.edit.modal').modal({
                onApprove: function () {
                  if ($('#ChannalEdit').form('is valid')) {
                        doEdit();
                        return false;
                  } else {
                    return false;
                  }
                }
              }).modal('show');
        }
    };

    // delete source records
    $scope.delSource = function () {
      var _sou = this.sou;
      $('#del-model .content p').html("确认删除" + _sou.name);
      $('.ui.small.del').modal({
        onApprove: function () {
          // $scope.$emit("BUSY");
          dataFactoryCRM.postDataByAPI('Source/remove', 'id=' + _sou.source_id)
            .then(function (d) {
              $scope.sourcercode = d.rcode;
              // if( d.rcode ) {
              //   $scope.sourceDataList();
              //   // return true;
              // }else{
              //   $scope.$emit("NOTBUSY");
              //   $scope.sourcerMsg = d.msg;
              //   $scope.msgAttention();
              //   // return false;
              // }
              // $scope.$emit("NOTBUSY");
              if (d.rcode) {
                $('.ui.modal1').modal('show');
              } else {
                $('.ui.modal2').modal('show');
              }
              $scope.Msg = "删除";
              setTimeout(function () {
                $('.time').modal('hide')
              }, 2000)
              $scope.sourceDataList();
            });
          return true;
        },
        onDeny: function () {
          return true;
        }
      }).modal('show');
    }
  });
});